package acom.imghandle.aa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//利用java 的反射机制，对事件监听的封装
public class EventHandler implements ActionListener{

	//组件所在地窗体对象
	private Object form;
	
	//受到委托的方法名
	private String methodName ;
	
	//构造函数
	public EventHandler(Object form,String methodName){
		this.form = form;
		this.methodName = methodName;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		Class formType = this.form.getClass();
		
		try {
			Method method = formType.getMethod(methodName, new Class[0]);
			method.invoke(this.form, new Object[0]);//这里的Object数组 是方法的参数
		} catch (SecurityException e1) {			
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {			
			e1.printStackTrace();
		} catch (IllegalArgumentException e2) {			
			e2.printStackTrace();
		} catch (IllegalAccessException e2) {			
			e2.printStackTrace();
		} catch (InvocationTargetException e2) {
			e2.printStackTrace();
		}
		
	}

}
